home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / support / initsocket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-18  |  1.8 KB  |  85 lines

  1. # include    <stdio.h>
  2. # include    <sys/types.h>
  3. # include    <sys/socket.h>
  4. # include    <sys/ioctl.h>
  5. # include    <netinet/in.h>
  6. # include    <arpa/inet.h>
  7. # include    <netdb.h>
  8. # include    <signal.h>
  9. # include    <sccs.h>
  10.  
  11. SCCSID(@(#)initsocket.c    8.2    6/12/88)
  12.  
  13. /*
  14. ** init_socket
  15. ** initilize the socket to the socket server
  16. */
  17. init_socket()
  18. {
  19.     register    int    from_socket;    /* file descriptor to attach to socket server */
  20.     int    to_ioctl = 1;            /* used in ioctl call */
  21.     struct    sockaddr_in    addr;        /* address where socket server is */
  22. #ifdef notdef
  23.     char    hostname[BUFSIZ];        /* hostname */
  24. #endif
  25.     struct    servent        *server;
  26.     struct    hostent        *myhost;
  27.     auto    int        len;
  28.     extern    int        errno;
  29.  
  30. #ifndef    DEBUG
  31.     if ( (len = fork()) != 0 )
  32.     {
  33. # ifdef    DEBUG
  34.         printf("lock driver becomes %d\n",len);
  35. # endif    DEBUG
  36.         if ( len == -1 )
  37.         {
  38.             perror("ingres lock driver, fork");
  39.             exit(errno);
  40.         }
  41.         exit(0);
  42.     }
  43. #endif
  44.     if ( (from_socket = socket(AF_INET,SOCK_STREAM,0)) == -1 )
  45.     {
  46. # ifdef    DEBUG
  47.         perror("INIT_S socket");
  48. # endif    DEBUG
  49.         exit(errno);
  50.     }
  51.     len = BUFSIZ;
  52.  
  53.     if ( (server = getservbyname("ingreslock",(char *)0)) == 0 )
  54.         exit(errno);
  55.  
  56. #ifdef notdef
  57.     gethostname(hostname,&len);
  58.  
  59.     if ( (myhost = gethostbyname(hostname)) == 0 )
  60.         exit(errno);
  61.     bzero((char *) &addr,sizeof (addr));
  62.     bcopy(myhost->h_addr,(char *)&addr.sin_addr,myhost->h_length);
  63. #endif
  64.     addr.sin_family = AF_INET;
  65.     addr.sin_port = server ? server->s_port : htons(1524);
  66.     len = sizeof (addr);
  67.     if ( bind(from_socket,(struct sockaddr *)&addr,len) == -1 )
  68.     {
  69. # ifdef    DEBUG
  70.         perror("INIT_S bind, assuming driver already running");
  71. # endif    DEBUG
  72.         exit(0);
  73.     }
  74.  
  75.     if ( listen(from_socket,10) == -1 )
  76.     {
  77.         perror("Ingres lock, can't listen on port");
  78.         exit(errno);
  79.     }
  80. #if 0
  81.     ioctl(from_socket,FIONBIO,&to_ioctl);
  82. #endif
  83.     return ( from_socket );
  84. }/* init_socket */
  85.